package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmb.bean.R;
import com.lmb.bean.ShoppingCart;
import com.lmb.service.ShoppingCartService;
import com.sun.prism.impl.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: ShoppingCartContorller
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/21 18:19
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartContorller {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R shoppingCartList(HttpSession session){
        Long id = (Long) session.getAttribute("user");
        System.out.println(id);
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(id!=null,ShoppingCart::getUserId,id);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R addshoppingCart(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }


    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){

        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        if(dishId != null){

            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{

            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne.getNumber() == 1){
            shoppingCartService.removeById(cartServiceOne.getId());
            cartServiceOne = shoppingCart;
        }else{
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        }

        return R.success(cartServiceOne);
    }

}
